//
//  AttendanceQueryViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/20.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "AttendanceQueryViewController.h"
#import "AttendanceModel.h"

static NSString *cellPath = @"AttendanceCell";
@interface AttendanceQueryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UILabel *yearLab;
@property (nonatomic, strong)UILabel *monthLab;
@property (nonatomic, assign)NSInteger year;
@property (nonatomic, assign)NSInteger month;
@property (nonatomic, assign)NSInteger firstYear;
@property (nonatomic, assign)NSInteger firstMonth;
@property (nonatomic, strong)NSArray *allDayAry;
@property (nonatomic, strong)UILabel *allMoneysLabel;

@end

@implementation AttendanceQueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self requestMyAttendance];
}

- (void)creatUI
{
    self.navigationItem.title = @"考勤查询";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; //初始化格式器。
    [formatter setDateFormat:@"YYYY-MM-dd"];//定义时间为这种格式： YYYY-MM-dd hh:mm:ss 。
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];
    NSArray *timeAry = [currentTime componentsSeparatedByString:@"-"];
    _year = [timeAry[0] integerValue];
    _firstYear = _year;
    _month = [timeAry[1] integerValue];
    _firstMonth = _month;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40*displaywidth)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    UIImageView *yearImgView = [ZCControl createImageViewWithFrame:CGRectMake((SCREEN_WIDTH/2-100*displaywidth)/2, 7.5*displaywidth, 100*displaywidth, 25*displaywidth) ImageName:@"xuankuang"];
    [topView addSubview:yearImgView];
    self.yearLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 0, 70*displaywidth, 25*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%ld年",_year] Color:RGB(128, 128, 128)];
    _yearLab.textAlignment = NSTextAlignmentCenter;
    [yearImgView addSubview:_yearLab];
    UIButton *lastYearBtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, 15*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(lastYearAction) Title:nil Font:12.0];
    lastYearBtn.backgroundColor = [UIColor clearColor];
    [yearImgView addSubview:lastYearBtn];
    UIButton *nextYearBtn = [ZCControl createButtonWithFrame:CGRectMake(85*displaywidth, 0, 15*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(nextYearAction) Title:nil Font:12.0];
    nextYearBtn.backgroundColor = [UIColor clearColor];
    [yearImgView addSubview:nextYearBtn];
    UIImageView *monthImgView = [ZCControl createImageViewWithFrame:CGRectMake((SCREEN_WIDTH/2-100*displaywidth)/2+SCREEN_WIDTH/2, 7.5*displaywidth, 100*displaywidth, 25*displaywidth) ImageName:@"xuankuang"];
    [topView addSubview:monthImgView];
    self.monthLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 0, 70*displaywidth, 25*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%ld月",_month] Color:RGB(128, 128, 128)];
    _monthLab.textAlignment = NSTextAlignmentCenter;
    [monthImgView addSubview:_monthLab];
    UIButton *lastMonthBtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, 15*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(lastMonthAction) Title:nil Font:12.0];
    lastMonthBtn.backgroundColor = [UIColor clearColor];
    [monthImgView addSubview:lastMonthBtn];
    UIButton *nextMonthBtn = [ZCControl createButtonWithFrame:CGRectMake(85*displaywidth, 0, 15*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(nextMonthAction) Title:nil Font:12.0];
    nextMonthBtn.backgroundColor = [UIColor clearColor];
    [monthImgView addSubview:nextMonthBtn];
    UIView *lineAView = [[UIView alloc] initWithFrame:CGRectMake(0, 39*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
    lineAView.backgroundColor = LinesColor;
    [topView addSubview:lineAView];
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 40*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-80*displaywidth) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [self.view addSubview:_tableview];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-40*displaywidth, SCREEN_WIDTH, 40*displaywidth)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    UILabel *allLabel = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-30*displaywidth-105*displaywidth, 0, 30*displaywidth, 40*displaywidth) Font:13 Text:@"合计:" Color:UIColorHexValue(0xFF333333)];
    [bottomView addSubview:allLabel];
    UILabel *allMoneyLabel = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-105*displaywidth, 0, 90*displaywidth, 40*displaywidth) Font:12 Text:@"" Color:UIColorHexValue(0xFFED1C24)];
    allMoneyLabel.textAlignment = NSTextAlignmentRight;
    [bottomView addSubview:allMoneyLabel];
    self.allMoneysLabel = allMoneyLabel;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allDayAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_allDayAry.count > 0)
    {
        AttendanceModel *model = _allDayAry[indexPath.row];
        UILabel *dayLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 5*displaywidth, 80*displaywidth, 35*displaywidth) Font:12.0 Text:model.checkDate Color:RGB(77, 77, 77)];
        [cell.contentView addSubview:dayLab];
        
        NSString *imgStr = @"zhengchang";
        NSString *titStr = @"  正常上班";
        if ([model.status integerValue] == 0)
        {
            imgStr = @"kaoq";
            titStr = @" 暂无考勤";
        }
        else if ([model.status integerValue] == 2)
        {
            imgStr = @"chid";
            titStr = @" 迟到";
        }
        else if ([model.status integerValue] == 3)
        {
            imgStr = @"queq";
            titStr = @" 缺勤";
        }
        UIButton *typeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-80*displaywidth, 5*displaywidth, 100*displaywidth, 35*displaywidth) ImageName:imgStr Target:nil Action:nil Title:titStr Font:12.0];
        [typeBtn setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        [cell.contentView addSubview:typeBtn];
        
        UIButton *hoursBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2+25*displaywidth, 5*displaywidth, 85*displaywidth, 35*displaywidth) ImageName:@"shizhong-" Target:nil Action:nil Title:[NSString stringWithFormat:@" %@小时",model.hours] Font:12.0];
        [hoursBtn setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        hoursBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        hoursBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [cell.contentView addSubview:hoursBtn];
        NSString *wage = @"0";
        if (![model.wage isEqualToString:@""])
        {
            wage = s_str(model.wage);
        }
        UILabel *wageLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-115*displaywidth, 5*displaywidth, 100*displaywidth, 35*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"+%@元",wage] Color:RGB(237, 28, 36)];
        wageLab.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:wageLab];
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45*displaywidth;
}

- (void)lastYearAction
{
    _year -= 1;
    _yearLab.text = [NSString stringWithFormat:@"%ld年",_year];
    [self requestMyAttendance];

}

- (void)nextYearAction
{
    if (_year == _firstYear)
    {
        return;
    }
    _year += 1;
    if (_year == _firstYear && _month > _firstMonth)
    {
        _month = _firstMonth;
    }
    _yearLab.text = [NSString stringWithFormat:@"%ld年",_year];
    _monthLab.text = [NSString stringWithFormat:@"%ld月",_month];
    [self requestMyAttendance];

    
}

- (void)lastMonthAction
{
    if (_month == 1)
    {
        _year--;
        _month = 12;
    }
    else
    {
        _month -= 1;
    }
    
    _monthLab.text = [NSString stringWithFormat:@"%ld月",_month];
    _yearLab.text = [NSString stringWithFormat:@"%ld年",_year];
   
    [self requestMyAttendance];

}

- (void)nextMonthAction
{
    if (_year == _firstYear && _month == _firstMonth)
    {
        return;
    }
    if (_month == 12)
    {
        _year++;
        _month = 1;
    }
    else
    {
        _month += 1;
    }
    _yearLab.text = [NSString stringWithFormat:@"%ld年",_year];
    _monthLab.text = [NSString stringWithFormat:@"%ld月",_month];
    [self requestMyAttendance];

    
}

- (void)requestMyAttendance
{
    WEAKSELF;
    NSDictionary *dic = @{@"year":[NSString stringWithFormat:@"%ld",_year],
                          @"month":[NSString stringWithFormat:@"%ld",_month]
                          };
    [HYBaseHandler requestMyAttendanceParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.allDayAry = [AttendanceModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            double money = 0.0;
            for (int i = 0; i<_allDayAry.count; i++) {
                AttendanceModel *model = _allDayAry[i];
                money += [model.wage doubleValue];
            }
            self.allMoneysLabel.text = [NSString stringWithFormat:@"%.2lf元", money];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
